/*------------------------------------------------------------------------------
#
#	Apple Macintosh Developer Technical Support
#
#	MultiFinder-Aware Simple TextEdit Sample Application
#
#	CPlusTESample
#
#	TApplication.h	-	C++ source
#
#	Copyright © 1989 Apple Computer, Inc.
#	All rights reserved.
#
#	Versions:	
#			1.00 					04/89
#
#	Components:
#			CPlusTESample.make		April 1, 1989
#			TApplicationCommon.h	April 1, 1989
#			TApplication.h			April 1, 1989
#			TDocument.h				April 1, 1989
#			TECommon.h				April 1, 1989
#			TESample.h				April 1, 1989
#			TEDocument.h			April 1, 1989
#			TApplication.cp			April 1, 1989
#			TDocument.cp			April 1, 1989
#			TESample.cp				April 1, 1989
#			TEDocument.cp			April 1, 1989
#			TESampleGlue.a			April 1, 1989
#			TApplication.r			April 1, 1989
#			TESample.r				April 1, 1989
#
#	CPlusTESample is an example application that demonstrates
#	how to initialize the commonly used toolbox managers,
#	operate successfully under MultiFinder, handle desk
#	accessories and create, grow, and zoom windows. The
#	fundamental TextEdit toolbox calls and TextEdit autoscroll
#	are demonstrated. It also shows how to create and maintain
#	scrollbar controls. 
#
#	This version of TESample has been substantially reworked in
#	C++ to show how a "typical" object oriented program could
#	be written. To this end, what was once a single source code
#	file has been restructured into a set of classes which
#	demonstrate the advantages of object-oriented programming.
#
------------------------------------------------------------------------------*/

#ifndef TAPPLICATION_H
#define TAPPLICATION_H

// Include necessary interface files
#include <Types.h>
#include <Desk.h>
#include <Events.h>
#include <OSUtils.h>

// we need resource ids
#include "TApplicationCommon.h"

// we need definitions of DocumentList class
#include "TDocument.h"

/*
	TApplication:

	This is our class which implements a basic Macintosh style program,
	including a MultiFinder-aware event loop. 
*/

// we derive from handle object to prevent fragmentation
class TApplication : HandleObject {
public:
	// Our constructor & destructor
	TApplication(void);

	// Call this routine to start event loop running
	void EventLoop(void);

	// Utility routines you can use
	Boolean TrapAvailable(short tNumber,TrapType tType);	// Is trap implemented???
	inline TDocumentList* DocList(void) { return fDocList; }

protected:
	// Returns total stack space required in bytes.
	// Returns 0 by default, which tells the initialization code
	// to use the default stack size.
	virtual long StackNeeded(void) { return 0; }
	// Returns total heap space required in bytes.
	// Returns 0 by default, which tells the initialization code
	// to use whatever heap size is given.
	virtual long HeapNeeded(void) { return 0; }

	// Loop control methods you may need to override
	virtual void SetUp(void) {}					// Run before event loop starts
	virtual void CleanUp(void) {}				// run at end of loop
	virtual void ExitLoop(void);				// to end loop, call this routine
	virtual void DoIdle(void) {}				// idle time handler (blink caret, background tasks)
	virtual void AdjustMenus(void) {}			// menu updater routine

	// event handlers you shouldn't need to override in a typical application
	virtual void DoOSEvent(void);				// Calls DoSuspend, DoResume and DoIdle as apropos
	virtual void DoMouseDown(void);				// Calls DoContent, DoGrow, DoZoom, etc
	virtual void DoKeyDown(void);				// also called for autokey events
	virtual void DoActivateEvt(void);			// handles setup, and calls DoActivate (below)
	virtual void DoUpdateEvt(void);				// handles setup, and calls DoUpdate (below)
	virtual void DoMouseInSysWindow(void) { SystemClick(&fTheEvent, fWhichWindow); }
	virtual void DoDrag(void);
	virtual void DoGoAway(void);				// handles setup, calls DoClose (below)

	// handlers you will need to override for functionality:
	
		// called by EventLoop and its handlers:
		virtual void AdjustCursor(void) {}		// cursor adjust routine, should setup mouseRgn
		virtual void DoMenuCommand(short menuID, short menuItem) {}
		// called by OSEvent (just calls DoActivate by default, so no clip conversion
		// is done). If you want to convert clipboard, override these routines
		virtual void DoSuspend(Boolean doClipConvert);
		virtual void DoResume(Boolean doClipConvert);
	
	// If you have an app that needs to know about these, override them
	virtual void DoMouseUp(void) {}
	virtual void DoDiskEvt(void) {}

	// Utility routines you need to provide to do MultiFinder stuff
	virtual unsigned long SleepVal(void) { return 0; }		// how long to sleep in WaitNextEvent

	// useful variables
	Boolean			fHaveWaitNextEvent;		// true if we have WaitNextEvent trap
	Boolean			fDone;					// set to true when we are ready to quit
	EventRecord		fTheEvent;				// our event record
	WindowPtr		fWhichWindow;			// currently active window
	Boolean			fInBackground;			// true if our app is suspended
	Boolean			fWantFrontClicks;		// true if we want front clicks
	RgnHandle		fMouseRgn;				// mouse moved region (set it in your DoIdle)
	TDocument*		fCurDoc;				// currently active document (if any)
	TDocumentList*	fDocList;				// the list of documents
};

// some other handy utility routines, not actually a part of application class

// display alert, using specified error STR# resource and error code as index
void AlertUser(short errResID, short errCode);
// call AlertUser to display error message, then quit...
void BigBadError(short errResID, short errCode);

#endif
